package com.framework.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.framework.core.domain.Control;
import com.framework.core.domain.Result;

public class InitConfigUtils {

	// 解释requestmapping文件--已弃用，改用注解
	public static Map<String, Object> getXMLRequestMappingConfig(String path) throws DocumentException {

		// 解析结果
		Map<String, Object> map = new HashMap<>();
		File f = new File(path);
		SAXReader reader = new SAXReader();
		Document doc = reader.read(f);
		Element root = doc.getRootElement();
		// 根节点下的所有节点
		Iterator<Element> iterator = root.elementIterator();
		// control封装
		Control control = null;
		while (iterator.hasNext()) {
			control = new Control();
			// 迭代子节点
			Element element = iterator.next();
			// ------可以根据节点名定位节点------
			// include节点
			if (element.getName().equals("mapping")) {
				Iterator<Attribute> attributeIterator = element.attributeIterator();
				while (attributeIterator.hasNext()) {
					Attribute next = attributeIterator.next();
					if ("name".equals(next.getName())) {
						control.setName(next.getValue().trim());
					}
					if ("class".equals(next.getName())) {
						control.setClazz(next.getValue().trim());
					}
					if ("method".equals(next.getName())) {
						control.setMethod(next.getValue().trim());
					}
				}
			}
			// element下是否有子节点
			Iterator<Element> e = element.elementIterator();
			Result result = null;
			Map<String, Result> results = new HashMap<>();
			while (e.hasNext()) {
				result = new Result();
				// 定位到下一个
				Element el = e.next();
				// 迭代所有属性
				Iterator<Attribute> attributeIterator = el.attributeIterator();
				while (attributeIterator.hasNext()) {
					Attribute next = attributeIterator.next();
					result.setName(next.getValue().trim());
					result.setPath(el.getStringValue().trim());
					results.put(result.getName(), result);
				}
			}
			control.setResults(results);
			map.put(control.getName(), control);
		}
		return map;
	}

	// 解释sqlconfig文件
	public static Map<String, Map<String, Object>> getXMLSqlMappingConfig(String xmlPath) throws DocumentException {

		// 所有待迭代文件
		List<File> lFiles = null;
		Map<String, Map<String, Object>> sumMap = new HashMap<>();
		try {
			lFiles = SqlScanXMLUtils.getXMLFile(xmlPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (File file : lFiles) {
			// 解析结果
			Map<String, Object> map = new HashMap<>();
			// File f = new File(path);
			SAXReader reader = new SAXReader();
			Document doc = reader.read(file);
			Element root = doc.getRootElement();
			// 获取namespace
			String namespace = root.attribute("namespace").getValue().trim();
			// 根节点下的所有节点
			Iterator<Element> iterator = root.elementIterator();
			while (iterator.hasNext()) {
				// 迭代子节点
				Element element = iterator.next();
				// ------可以根据节点名定位节点------
				// include节点
				if (element.getName().equals("sql")) {
					Iterator<Attribute> attributeIterator = element.attributeIterator();
					while (attributeIterator.hasNext()) {
						Attribute next = attributeIterator.next();
						if ("id".equals(next.getName())) {
							map.put(next.getValue().trim(), element.getStringValue().trim());
						}
					}
				}

			}
			sumMap.put(namespace, map);
		}

		return sumMap;
	}

	// 解释properties配置文件
	public static Map<String, Object> getPropertiesConfig(String path) throws FileNotFoundException, IOException {

		Map<String, Object> map = new HashMap<>();
		Properties properties = new Properties();
		// 加载文件
		properties.load(new FileInputStream(path));
		Enumeration<?> enum1 = properties.propertyNames();
		while (enum1.hasMoreElements()) {
			String strKey = (String) enum1.nextElement();
			String strValue = properties.getProperty(strKey);
			map.put(strKey, strValue);
		}
		return map;

	}

}
